<?php
	if ( !isset($_GET['page']) )
	{
		$pg = 1;
	}
	else
	{
		$pg = $_GET['page'];
	}

	if ( preg_match('/\d/', $pg) ) 
	{
		$page = ( $pg - 1 ) * 10;
		$cache_name = 'top_guild_' . $pg;
		
		if ( !$eeRO_cache->check($cache_name, 1800) )
		{
			$temp = array();
			
			$query = $eeRO_base_ragnarok->prepare('SELECT `name`, `master`, `guild_lv`, `connect_member`, `average_lv` FROM `guild` ORDER BY `guild_lv` DESC LIMIT :page, 11');
			$query->bindValue(':page', $page, PDO::PARAM_INT);
			$query->execute();

			if ( $query->rowCount() > 0 )
			{
				while ( $guild = $query->fetch() )
				{
					$temp[] = $guild;
				}
			}
			else
			{
				$temp = null;
			}
			
			if ( sizeof($temp) > 10 )
			{
 				unset($temp[10]); 
				$flags['next'] = true; 
			}
			else
			{
				$flags['next'] = false;
			}
			
			if ( $pg == 0 || $pg == 1 )
			{
				$pg = 1;
				$flags['prev'] = false;
			}
			else
			{
				$flags['prev'] = true;
			}
			
			$flags['page'] = $pg;
			$eeRO_cache->set($cache_name.'_p',$flags);
			$eeRO_cache->set($cache_name, $temp);
		}
		
		$eeRO_view->assign('flags',$eeRO_cache->get($cache_name.'_p'));
		$eeRO_view->assign('module', array('title' => 'Рейтинг гильдий', 'template' => 'top_guild.html', 'content' => $eeRO_cache->get($cache_name), 'content_size' => sizeof($eeRO_cache->get($cache_name))));
	}
	else
	{
		$eeRO_view->assign('module', array('template' => 'bootstrap_404.html'));
	}